package com.gy.zpchat.ui.friend.search.ui.adapter;

import android.annotation.SuppressLint;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.RoundedCorners;
import com.bumptech.glide.request.RequestOptions;
import im.zego.zim.entity.ZIMFriendInfo;
import com.gy.zpchat.R;
import java.util.ArrayList;
import java.util.List;

public class SearchFriendListAdapter extends RecyclerView.Adapter {

    private OnItemClickListener onItemClickListener;
    private OnItemLongClickListener onItemLongClickListener;
    private final List<ZIMFriendInfo> friendList = new ArrayList<>();

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.item_friend_content, parent, false);
        return new ZIMFriendViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof ZIMFriendViewHolder) {
            final ZIMFriendViewHolder zimFriendViewHolder = (ZIMFriendViewHolder)holder;

            ZIMFriendInfo friendInfo = friendList.get(position);
            zimFriendViewHolder.setFriendInfo(friendInfo);

            if (onItemClickListener != null) {
                zimFriendViewHolder.list.setOnClickListener(
                    v -> { onItemClickListener.onItemClick(v, friendInfo); });
            }

            if (onItemLongClickListener != null) {
                zimFriendViewHolder.list.setOnLongClickListener(new View.OnLongClickListener() {
                    @Override
                    public boolean onLongClick(View v) {
                        onItemLongClickListener.onItemRemove(v, friendInfo);
                        return true;
                    }
                });
            }
        }
    }

    @Override
    public int getItemCount() {
        return friendList.size();
    }

    public void setOnItemClickListener(OnItemClickListener mOnItemClickListener) {
        this.onItemClickListener = mOnItemClickListener;
    }

    @SuppressLint("NotifyDataSetChanged")
    public void addAll(ArrayList<ZIMFriendInfo> friendList) {
        this.friendList.clear();
        this.friendList.addAll(friendList);
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void add(ArrayList<ZIMFriendInfo> friendList) {
        this.friendList.addAll(friendList);
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void delete(ArrayList<ZIMFriendInfo> friendInfoList) {
        List<ZIMFriendInfo> tmpFriendList = friendList;
        for (int i = 0; i < tmpFriendList.size(); i++) {
            for (int d = 0; d < friendInfoList.size(); d++) {
                if (tmpFriendList.get(i).userID.equals(friendInfoList.get(d).userID)) {
                    friendList.remove(i);
                }
            }
        }
        notifyDataSetChanged();
    }

    @SuppressLint("NotifyDataSetChanged")
    public void update(ArrayList<ZIMFriendInfo> friendInfoList) {
        List<ZIMFriendInfo> tmpFriendList = friendList;
        for (int i = 0; i < tmpFriendList.size(); i++) {
            for (int d = 0; d < friendInfoList.size(); d++) {
                if (tmpFriendList.get(i).userID.equals(friendInfoList.get(d).userID)) {
                    friendList.set(i, friendInfoList.get(d));
                }
            }
        }
        notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        void onItemClick(View view, ZIMFriendInfo zimFriendInfo);
    }

    public void setOnItemLongClickListener(OnItemLongClickListener onItemLongClickListener) {
        this.onItemLongClickListener = onItemLongClickListener;
    }

    public interface OnItemLongClickListener {
        void onItemRemove(View view, ZIMFriendInfo zimFriendInfo);
    }

    public interface HeaderItemClickListener {
        void onItemFriendRequestList();

        void onItemBlacklist();
    }

    public static class ZIMFriendViewHolder extends RecyclerView.ViewHolder {
        TextView userNameTextView;
        ImageView avatarImageView;
        RelativeLayout list;

        TextView tvReaction;

        public ZIMFriendViewHolder(View itemView) {
            super(itemView);
            list = itemView.findViewById(R.id.list);
            userNameTextView = itemView.findViewById(R.id.user_name);
            avatarImageView = itemView.findViewById(R.id.avatar);
        }

        public void setFriendInfo(ZIMFriendInfo friendInfo) {
            String userName = friendInfo.userName;
            if (!"".equals(friendInfo.friendAlias)) {
                userNameTextView.setText(friendInfo.friendAlias);
            } else if (!"".equals(userName)) {
                userNameTextView.setText(userName);
            } else if (!"".equals(friendInfo.userID)) {
                userNameTextView.setText(friendInfo.userID);
            }

            Glide.with(this.avatarImageView.getContext())
                .load(friendInfo.userAvatarUrl)
                .apply(RequestOptions.bitmapTransform(new RoundedCorners(30)))
                .into(avatarImageView);
        }
    }

    public static class ZIMHeaderViewHolder extends RecyclerView.ViewHolder {

        ImageView addFriendImage;
        ImageView blacklist;
        View friendRequestListLayout;
        View blacklistLayout;

        public ZIMHeaderViewHolder(View itemView) {
            super(itemView);
            friendRequestListLayout = itemView.findViewById(R.id.friend_request_list_layout);
            blacklistLayout = itemView.findViewById(R.id.blacklist_layout);

            addFriendImage = itemView.findViewById(R.id.ic_add_friend);
            blacklist = itemView.findViewById(R.id.ic_blacklist);
        }

        public void initView() {
            Glide.with(this.blacklist.getContext()).load(R.drawable.ic_blacklist).into(blacklist);
            Glide.with(this.addFriendImage.getContext())
                .load(R.drawable.icon_add_friends)
                .into(addFriendImage);
        }
    }
}
